package com.net.wanjian.networkhospitalmanager.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.net.wanjian.networkhospitalmanager.MyApplication;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.consts.SharedPreferencesKeyConst;

/**
 * Description:本地保存类
 * Created by lidongcai on 2017/4/22.
 * Email:  lidongcaiocc@163.com
 */

public class SharedXmlUtil {
    private static volatile SharedXmlUtil mSharedXmlUtil;
    private SharedPreferences mShared;
    private SharedPreferences.Editor mEditor;

    private SharedXmlUtil() {
    }

    public static SharedXmlUtil getInstance(){
        if (mSharedXmlUtil == null){
            synchronized (SharedXmlUtil.class){
                if (mSharedXmlUtil == null){
                    mSharedXmlUtil = new SharedXmlUtil(MyApplication.getContext());
                }
            }
        }
        return mSharedXmlUtil;
    }

    public SharedXmlUtil(Context context, String filename) {
        mShared = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
        mEditor = mShared.edit();
    }

    private SharedXmlUtil(Context context) {
        mShared = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE);
        mEditor = mShared.edit();
    }

    public void write(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }

    public void write(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    public void write(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    public void write(String key, float value) {
        mEditor.putFloat(key, value);
        mEditor.commit();
    }

    public void write(String key, long value) {
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    public String read(String key, String defValue) {
        return mShared.getString(key, defValue);
    }

    public boolean read(String key, boolean defValue) {
        return mShared.getBoolean(key, defValue);
    }

    public int read(String key, int defValue) {
        return mShared.getInt(key, defValue);
    }

    public float read(String key, float defValue) {
        return mShared.getFloat(key, defValue);
    }

    public long read(String key, long defValue) {
        return mShared.getLong(key, defValue);
    }

    public void delete(String key) {
        mEditor.remove(key);
        mEditor.commit();
    }

    /**
     * 保存token
     * @param token
     */
    public void setToken(String token){
        mEditor.putString(SharedPreferencesKeyConst.TOKEN_KEY,token);
        mEditor.commit();
    }

    public String getToken(){
        return mShared.getString(SharedPreferencesKeyConst.TOKEN_KEY,"");
    }

    public String getDeviceId(){
        return mShared.getString(SharedPreferencesKeyConst.DEVICE_IDENTITY_KEY,"");
    }

    public String getHospitalId(){
        return mShared.getString(SharedPreferencesKeyConst.SELECT_HOSPITAL_KEY,"");
    }

    public String getUserInfoId(){
        return mShared.getString(SharedPreferencesKeyConst.USER_INFO_ID_KEY,"");
    }

    //获取用户角色
    public String getUserIdentity(){
        return mShared.getString(SharedPreferencesKeyConst.User_Identity_Tag_key,"");
    }

    //获取用户身份ID
    public String getUserIdentityId(){
        return mShared.getString(SharedPreferencesKeyConst.User_Identity_ID_key,"");
    }

    //获取消息通知标签
    public String getMessageTag(){
        return mShared.getString(SharedPreferencesKeyConst.MESSAGE_TAG_KEY,"");
    }
    //获取视频ID
    public String getVideoId(){
        return mShared.getString(SharedPreferencesKeyConst.SELECT_ALI_VIDEO_ID,"");
    }
}
